<% minimal_proxy_template = if assigns[:custom_abi], do: nil, else: Chain.get_minimal_proxy_template(@address.hash) %>
<% metadata_for_verification = if assigns[:custom_abi], do: nil, else: minimal_proxy_template || Chain.get_address_verified_twin_contract(@address.hash).verified_contract %>
<% smart_contract_verified = if assigns[:custom_abi], do: false, else: BlockScoutWeb.AddressView.smart_contract_verified?(@address) %>
<%= unless smart_contract_verified do %>
  <%= if metadata_for_verification do %>
    <%= if minimal_proxy_template do %>
      <%= render BlockScoutWeb.CommonComponentsView, "_minimal_proxy_pattern.html", address_hash: metadata_for_verification.address_hash, conn: @conn %>
    <% else %>
      <% path = address_verify_contract_path(@conn, :new, @address.hash) %>
      <div class="mb-4">
        <%= render BlockScoutWeb.CommonComponentsView, "_info.html" %><span> <%= gettext("Contract is not verified. However, we found a verified contract with the same bytecode in Blockscout DB") %> <%= link(
  metadata_for_verification.address_hash,
  to: address_contract_path(@conn, :index, metadata_for_verification.address_hash)) %>.<br/> <%= gettext("All functions displayed below are from ABI of that contract. In order to verify current contract, proceed with") %> <%= link(
        gettext("Verify & Publish"),
        to: path
      ) %> <%= gettext("page") %></span>
      </div>
    <% end %>
  <% end %>
<% end %>
<%= if smart_contract_verified do %>
  <%= if @address.smart_contract.is_changed_bytecode do %>
    <%= render BlockScoutWeb.CommonComponentsView, "_changed_bytecode_warning.html" %>
  <% else %>
    <div class="d-none" data-selector="hidden-bytecode-warning">
      <%= render BlockScoutWeb.CommonComponentsView, "_changed_bytecode_warning.html" %>
    </div>
  <% end %>
<% end %>
<%= if @contract_type == "proxy" do %>
<div class="implementation-container">
    <h2 class="implementation-title">Implementation address: </h2><h3  class="implementation-value"><%= link(
                  @implementation_address,
                  to: address_path(@conn, :show, @implementation_address)
                ) %></h3>
</div>
<% end %>
<%= for {function, counter} <- Enum.with_index(@read_only_functions ++ @read_functions_required_wallet, 1) do %>
  <div class="d-flex py-2 border-bottom" data-function<%= if assigns[:custom_abi], do: "-custom" %>>
    <div class="py-2 pr-2 text-nowrap">
      <%= counter %>.
      <%= case function["type"] do %>
        <% "fallback" -> %>
          <%= gettext "fallback" %><%= render BlockScoutWeb.CommonComponentsView, "_i_tooltip_2.html", text: gettext("The fallback function is executed on a call to the contract if none of the other functions match the given function signature, or if no data was supplied at all and there is no receive Ether function. The fallback function always receives data, but in order to also receive Ether it must be marked payable.") %>
        <% "receive" -> %>
          <%= gettext "receive" %><%= render BlockScoutWeb.CommonComponentsView, "_i_tooltip_2.html", text: gettext("The receive function is executed on a call to the contract with empty calldata. This is the function that is executed on plain Ether transfers (e.g. via .send() or .transfer()). If no such function exists, but a payable fallback function exists, the fallback function will be called on a plain Ether transfer. If neither a receive Ether nor a payable fallback function is present, the contract cannot receive Ether through regular transactions and throws an exception.") %>
        <% _ -> %>
          <%= function["name"] %>
      <% end %>
      &#8594;
    </div>

    <%= if queryable?(function["inputs"]) || writable?(function) || Helper.read_with_wallet_method?(function) do %>
      <div style="width: 100%; overflow: hidden;">
        <% function_abi = 
                    case Jason.encode([function]) do 
                      {:ok, abi_string} ->
                        abi_string
                      _ ->
                        if @contract_type == "proxy" do
                          @implementation_abi
                        else
                          @contract_abi
                        end 
                    end %>
        <form class="form-inline" data-function-form data-action="<%= if @action == "write", do: :write, else: :read %>" data-type="<%= @contract_type %>" data-url="<%= smart_contract_path(@conn, :show, Address.checksum(@address.hash)) %>" data-contract-address="<%= @address.hash %>" data-contract-abi="<%= function_abi %>" data-implementation-abi="<%= function_abi %>" data-chain-id="<%= Application.get_env(:block_scout_web, :chain_id) %>" data-custom-abi="<%= if assigns[:custom_abi], do: true, else: false %>">
          <input type="hidden" name="function_name" value='<%= function["name"] %>' />
          <input type="hidden" name="method_id" value='<%= function["method_id"] %>' />

          <%= if queryable?(function["inputs"]) do %>
            <%= for input <- function["inputs"] do %>
              <div class="form-group pr-3 d-flex" style="margin: 0.5rem 0 !important;">
                <%= if int?(input["type"]) do %>
                  <input type="number" name="function_input" class="form-control form-control-sm address-input-sm"
                  placeholder='<%= input["name"] %>(<%= input["type"] %>)'
                  style="width: <%= (String.length(input["name"]) + String.length(input["type"]) + 2) * 10 %>px;"/>
                  <span data-dropdown-toggle="" data-toggle="dropdown">
                    <span class="button btn-line button-xs contract-plus-btn-container ml-1">
                      <i class="fa fa-plus contract-plus-btn"></i>
                    </span>
                    <div class="dropdown-menu exponention-dropdown">  
                      <div class="dropdown-item contract-exponentiation-btn" data-power=6>10<sup>6</sup></div>
                      <div class="dropdown-item contract-exponentiation-btn" data-power=8>10<sup>8</sup></div>
                      <div class="dropdown-item contract-exponentiation-btn" data-power=18>10<sup>18</sup></div>
                      <div class="dropdown-item contract-exponentiation-btn" data-power><input type="number" name="custom_power" class="form-control form-control-sm address-input-sm ml-1 custom-power-input" />10</div> 
                    </div>
                  </span>

                <% else %>
                  <input type="text" name="function_input" class="form-control form-control-sm address-input-sm" 
                  placeholder='<%= input["name"] %>(<%= input["type"] %>)' 
                  size="<%= String.length(input["name"]) + String.length(input["type"]) + 2 %>" />
                <% end %>
              </div>
            <% end %>
          <% end %>

          <%= if Helper.payable?(function) do %>
            <div class="form-group pr-3 d-flex">
              <input type="number" name="function_input" tx-value
              data-toggle="tooltip" title='Amount in native token <<%= Explorer.coin_name() %>>' class="form-control form-control-sm address-input-sm" placeholder='value(<%= Explorer.coin_name() %>)' min="0" step="1e-18" />
            </div>
          <% end %>

          <div>
            <input type="submit" value='<%= if @action == "write", do: gettext("Write"), else: gettext("Query")%>' class="button btn-line button-xs py-0 write-contract-btn" />
          </div>
        </form>

        <div class="alert alert-danger py-2 word-break-all" style="margin-bottom: 0; display: none;" input-parse-error-container></div>
        <%= if outputs?(function["outputs"]) do %>
          <div class='p-2 text-muted <%= if (queryable?(function["inputs"]) == true), do: "w-100" %>'>
            <%= if (queryable?(function["inputs"])), do: raw "&#8627;" %>

            <%= for output <- function["outputs"] do %>
              <%= if output["name"] && output["name"] !== "", do: "#{output["name"]}(#{output["type"]})", else: output["type"] %>
            <% end %>
          </div>
        <% end %>
        <div data-function-response></div>
      </div>
    <% else %>
      <%= cond do %>
        <% outputs?(function["outputs"]) -> %>
          <div class="align-self-center">
            <% length = Enum.count(function["outputs"]) %>
            <%= for {output, index} <- Enum.with_index(function["outputs"]) do %>
              <%= if address?(output["type"]) do %>
                <div class="py-2 word-break-all">
                  <%= link(
                    output["value"],
                    to: address_path(@conn, :show, output["value"])) %><%= if not_last_element?(length, index) do %>,&#160;<% end %>
                </div>
              <% else %>
                <%= if output["type"] == "uint256" do %>
                  <div class="py-2 word-break-all">
                    <div data-wei-ether-converter>
                      <i>(uint256):</i>
                      <span data-conversion-unit data-original-value="<%= output["value"] %>"><%= output["value"] %></span>
                      <span class="py-2 px-2">
                        <input class="wei-ether" type="checkbox" autocomplete="off">
                        <span class="d-inline-block" data-conversion-text-wei><%= gettext("WEI")%></span>
                        <span class="d-none" data-conversion-text-eth><%= Explorer.coin_name() %></span>
                      </span>
                    </div>
                  </div> 
                <% else %>
                  <div class="align-self-center function-output word-break-all <%= if not_last_element?(length, index), do: "mb-1" %>"><%= raw(values_with_type(output["value"], output["type"], [output["name"]], 0, output["components"])) %></div>
                <% end %>
              <% end %>
            <% end %>
          </div>
        <% error?(function["outputs"]) -> %>
          <% {:error, text_error} = function["outputs"] %>
          <div class="alert alert-danger py-2 word-break-all" style="margin-bottom: 0;"><%= text_error %></div>
        <% true -> %>
          <% nil %>
      <% end %>
    <% end %>
  </div>
<% end %>